package com.engine.qlExpress.context;


import com.engine.core.domain.CustomQuery;
import com.engine.core.gen.Global;
import com.engine.core.interceptor.CommandContext;
import com.engine.core.util.Response;
import com.engine.service.CustomService;
import com.engine.system.User;
import com.ql.util.express.ExpressRunner;

import java.util.List;
import java.util.Map;

public class QleJdbcCommandContext extends CommandContext {

    /**
     * 最后一次action的返回值
     */
    private Response lastResponse=new Response();

    private Response result=new Response();

    private CustomService customService;


    public QleJdbcCommandContext(Global global, CustomService customService) {
        super(global);
        this.put("lastResp",lastResponse);
        this.put("result",result);
        this.customService=customService;
    }


    public void error(int code,String message) {
        result.setSuccess(false);
        result.setMsg(message);
        result.setCode(code);
    }

    public void errorData(int code,String message,Map<String,Object> datas) {
        result.setSuccess(false);
        result.setMsg(message);
        result.setCode(code);
        result.setMapValues(datas);
    }


    public void success(int code,String message) {
        result.setSuccess(true);
        result.setMsg(message);
        result.setCode(code);
    }


    public void successMap(int code,Map<String,Object> datas) {
        result.setSuccess(true);
        result.setCode(code);
        result.set(Response.RESPONSEDATA, datas);
    }

    public void successList(int code, List datas) {
        result.setSuccess(true);
        result.setCode(code);
        result.set(Response.RESPONSEDATA, datas);
    }


    public void resultMap(Map<String,Object> datas) {
        result.setSuccess(true);
        result.set(Response.RESPONSEDATA, datas);
    }

    public void resultAttr(String key, Object val) {
        result.setSuccess(true);
        result.set(key, val);
    }

    public void resultList(List datas) {
        result.setSuccess(true);
        result.set(Response.RESPONSEDATA, datas);
    }


    public void execName(String queryName,String resourceNamespace,Map<String,Object> params){
        CustomQuery customQuery= customService.getCustomQuery(-1,queryName,resourceNamespace);
        Response result = customService.queryById(customQuery,new User(),params);
        lastResponse.setAll(result);//记录执行结果
    }

    public void execId(int name,Map<String,Object> params){
        CustomQuery customQuery= customService.getCustomQuery(-1,null,null);
        Response result = customService.queryById(customQuery,new User(),params);
        lastResponse.setAll(result);//记录执行结果
    }


    public  ExpressRunner getRunnerIns() throws Exception{
        ExpressRunner ins = new ExpressRunner();
        ins.addFunctionOfServiceMethod("ctx_execName", this, "execName", new Class[]{String.class, String.class,Map.class}, "QleContext#execName error！");
        ins.addFunctionOfServiceMethod("ctx_execId", this, "execId", new Class[]{Integer.class,Map.class}, "QleContext#execId error！");
        return ins;
    }
}
